package cn.nfwx.tv.fragment.dialog;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.nfwx.tv.R;
import cn.nfwx.tv.widget.CustomStatusView;

/**
 * Created by zhouzechao on 2018/1/30.
 */

public class WifiNoteFragment extends CommonDialogFragment {
    protected static final String EXTRA_NAME_CANCELABLE = "cancelable";
    protected static final String EXTRA_NAME_STATS = "stats";
    protected TextView mTitleTv, cancle_btn;
    protected TextView mMessageTv;
    protected Button mConfirm;
    protected RelativeLayout ll_wifi;
    protected CustomStatusView customStatusView;

    private onWifiNoteClick onWifiNoteClick;

    public void setOnWifiNoteClick(WifiNoteFragment.onWifiNoteClick onWifiNoteClick) {
        this.onWifiNoteClick = onWifiNoteClick;
    }

    public interface onWifiNoteClick {
        void tryagain();
    }

    public static WifiNoteFragment newInstance(boolean cancelable) {
        WifiNoteFragment instance = new WifiNoteFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_NAME_CANCELABLE, cancelable);
        instance.setArguments(bundle);
        return instance;
    }

    public static WifiNoteFragment newInstance(int stats) {
        WifiNoteFragment instance = new WifiNoteFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_NAME_CANCELABLE, true);
        bundle.putInt(EXTRA_NAME_STATS, stats);
        instance.setArguments(bundle);
        return instance;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_wifi_note;
    }

    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ll_wifi = findView(R.id.ll_wifi);
        mTitleTv = findView(R.id.confirm_tv_title);
        customStatusView = findView(R.id.wifi_load);
        mMessageTv = findView(R.id.confirm_tv_message);
        mConfirm = findView(R.id.confirm_btn_positive);
        cancle_btn = findView(R.id.cancle_btn);
        init();
    }

    private void init() {
        //是否允许关闭
        int status = getArguments().getInt(EXTRA_NAME_STATS, 1);
        boolean cancelable = getArguments().getBoolean(EXTRA_NAME_CANCELABLE, true);
        setCancelable(cancelable);
        ll_wifi.setClickable(true);
        cancle_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onWifiNoteClick != null) {
                    onWifiNoteClick.tryagain();
                }
            }
        });
        if (status == 1) {
            loadLoading();
        } else if (status == 2) {
            loadFailure();
        }
    }

    public void loadLoading() {
        if (customStatusView != null) {
            mConfirm.setVisibility(View.GONE);
            mTitleTv.setText(getText(R.string.wifi_search));
            cancle_btn.setVisibility(View.GONE);
            customStatusView.loadLoading();
        }
    }

    public void loadFailure() {
        if (customStatusView != null) {
            mConfirm.setVisibility(View.VISIBLE);
            mTitleTv.setText(getText(R.string.wifi_note_no_title));
            cancle_btn.setVisibility(View.VISIBLE);
            customStatusView.loadFailure();
        }
    }

    @Override
    protected boolean isOutsideTouchable() {
        return true;
    }

    @Override
    protected boolean isTranslucentBackground() {
        return false;
    }
}